package com.one.group.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.admin.intercepter.AdminThreadLocal;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.VisitorDto;
import com.one.group.model.dto.VisitorQueryDto;
import com.one.group.model.entity.Visitor;
import com.one.group.model.vo.VisitorListVo;
import com.one.group.result.Result;
import com.one.group.service.VisitorService;
import com.one.group.utils.ResultUtil;
import com.one.group.utils.ThrowUtil;
import com.one.group.utils.WebUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wanbo
 * @Date 2025/9/28 16:50
 */
@RestController
@RequestMapping("/visitor")
@Slf4j
@Tag(name = "访客相关接口")
public class VisitorController {

    @Autowired
    private VisitorService visitorService;

    @PostMapping("/list")
    @Operation(summary = "分页查询访客列表")
    public Result findVisitorByQueryDto(@RequestBody VisitorQueryDto visitorQueryDto) {
        //1.准备分页条件
        PageHelper.startPage(visitorQueryDto.getPage(),visitorQueryDto.getLimit());

        //查询
        List<VisitorListVo> visitorList = visitorService.findVisitorByQueryDto(visitorQueryDto);

        //2. 获取分页结果
        PageInfo<VisitorListVo> pageInfo = new PageInfo<>(visitorList);

        return ResultUtil.success(pageInfo.getList());
    }

    @PostMapping("/add")
    @Operation(summary = "添加访客")
    public Result addVisitor(@RequestBody @Valid VisitorDto visitorDto, BindingResult bindingResult) {
        // TODO 参数校验
        //1.校验
        if(bindingResult.hasErrors()){
            System.out.println("参数有误");
            return WebUtils.getResult(bindingResult);
        }

        // 拷贝参数到另一个对象中
        Visitor visitor = new Visitor();
        BeanUtil.copyProperties(visitorDto, visitor);

        boolean result = visitorService.save(visitor);

        log.info("添加访客结果：{}", result);

        return result ? ResultUtil.success() : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @GetMapping("/delete")
    public Result deleteVisitorById(Long id){
        ThrowUtil.throwIf(id==null, ErrorCode.PARAMS_ERROR);
        boolean result = visitorService.removeById(id);
        return result ? ResultUtil.success() : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @PostMapping("/update")
    @Operation(summary = "修改访客信息")
    public Result updateVisitorById(@RequestBody Visitor visitor, BindingResult bindingResult){
        ThrowUtil.throwIf(visitor.getId()==null, ErrorCode.PARAMS_ERROR);
        boolean result = visitorService.updateById(visitor);
        return result ? ResultUtil.success() : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

}
